Welcome back. One more thing to note about dictionaries is aliasing.
So, on line one,
we create a dictionary that has three key value pairs.
On line two, we assign a new variable called alias to be that dictionary.
On line four, we print out is alias opposites.
This is asking, are they pointing to the same object?
So, if we run CodeLens on lines one through four,
then we'll see again line one creates a dictionary and
line two assigned alias to point to the exact same object of that dictionary.
Now, this is important because here,
if we say alias sub right equals left,
we're assigning right from its previous value which was wrong,
and then we print out opposites sub right,
then we're actually going to print out left on line seven.
So, here, we can see line four says true because alias is the same object as opposites.
But now, when we print out opposites sub right,
we now get left.
That's despite the fact that we first assigned opposite sub right to be wrong,
and in all of these lines,
we never directly changed opposites sub right.
But the culprit here is that on line six,
when we changed alias sub right to be left,
then that also have changed opposite sub right.
Again, we can use CodeLens to see why that is.
So, on line one,
we create a dictionary and then we assign alias to be that exact same dictionary.
We can see alias is opposites.
Now, when we assign alias sub right to now be left,
then that modifies that dictionary.
So, if that right is now left and that change
the value associated with the key right to be left for both opposites and alias.
So, let's go through a question related to this.
So, here, on the first line,
we create a dictionary with
four key value pairs and then we assign yourdict to be my dictionary.
So, yourdict is pointing to the same dictionary.
Then, we say yourdict sub elephant equals 999.
Now, remember, yourdict and mydict are the same object here.
So, when we assign it elephant to be 999,
that erases the value 23 and replaces it with
999 for both of these dictionaries because they're pointing at the same object.
So, now, when we print out mydictionary sub elephant,
then we're going to get the value 999.
That's all for now, until next time.